﻿using QldtSdh.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QldtSdh.BLL
{
    class BizPhanQuyen
    {

        public static List<string> GetListByUser(string userName)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.AppUser_FunctionDbSet.Where(e => e.UserName == userName).Select(e => e.FunctionName).ToList();
            }
        }

        public static CODE_RESULT_RETURN AddOrDelete(string userName, string functionName)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var item = db.AppUser_FunctionDbSet.Where(e => e.UserName == userName && e.FunctionName == functionName).FirstOrDefault();
                if (item != null)
                {
                    db.AppUser_FunctionDbSet.Remove(item);
                }
                else
                {
                    AppUser_Function pq = new AppUser_Function { UserName = userName, FunctionName = functionName };
                    db.AppUser_FunctionDbSet.Add(pq);
                }
                db.SaveChanges();
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static List<ToolStripMenuItem> GetItems(MenuStrip menuStrip)
        {
            List<ToolStripMenuItem> myItems = new List<ToolStripMenuItem>();
            foreach (ToolStripMenuItem i in menuStrip.Items)
            {
                GetMenuItems(i, myItems);
            }
            return myItems;
        }

        private static void GetMenuItems(ToolStripMenuItem item, List<ToolStripMenuItem> items)
        {
            items.Add(item);
            foreach (ToolStripItem i in item.DropDownItems)
            {
                if (i is ToolStripMenuItem)
                {
                    GetMenuItems((ToolStripMenuItem)i, items);
                }
            }
        }
    }
}
